package com.qc31.monitorlib.ui.video

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.view.Window
import com.shuyu.gsyvideoplayer.GSYVideoBaseManager
import com.shuyu.gsyvideoplayer.model.VideoOptionModel
import com.shuyu.gsyvideoplayer.player.IPlayerManager
import com.shuyu.gsyvideoplayer.player.IjkPlayerManager
import com.shuyu.gsyvideoplayer.utils.CommonUtil
import tv.danmaku.ijk.media.player.IjkMediaPlayer


/**
 *@Author Jasper
 *@Time   2020/10/10
 *@Desc   自定义播放管理类
 */
class CustomVideoManager : GSYVideoBaseManager() {
    companion object {
        val instance: CustomVideoManager
                by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
                    CustomVideoManager()
                }
    }

    init {
        init()
        initVideoOption()
        isNeedMute = true
    }

    /**
     * 设置Ijkplayer 参数
     */
    private fun initVideoOption() {
        val list = ArrayList<VideoOptionModel>()
//        //开启硬解码
        list.add(VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec", 1))
////        //开启软解码
//        list.add(VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "videotoolbox", 0))
        //过小会延迟
        list.add(VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "buffer_size", 10240))
        list.add(VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "rtsp_transport", "tcp"))
        //无限读
        list.add(VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "infbuf", 1))
        //分析码流时长
        list.add(VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "analyzemaxduration", 100))
        //  自动开始播放已准备好的
        list.add(VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "start-on-prepared", 0))
        //默认值48
        list.add(VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_CODEC, "skip_loop_filter", 48))
        //不设置会加长延迟
        list.add(VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "probesize", 10240))
        // 清空DNS,有时因为在APP里面要播放多种类型的视频(如:MP4,直播,直播平台保存的视频,和其它http视频), 有时会造成因为DNS的问题而报10000问题的
        list.add(VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "dns_cache_clear", 1))
        list.add(VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "dns_cache_timeout", -1))

        list.add(VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "flush_packets", 1))
        //最大缓存数
//        list.add(VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "max-buffer-size", 0))
//        //默认最小帧数2
//        list.add(VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "min-frames", 2))
        //丢帧,太卡可以尝试丢帧
        list.add(VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "framedrop", 3))
        //关闭播放器缓冲，这个必须关闭，否则会出现播放一段时间后，一直卡主，控制台打印 FFP_MSG_BUFFERING_START
        list.add(VideoOptionModel(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "packet-buffering", 0))
        setOptionModelList(list)
    }

    /**
     * 退出全屏，主要用于返回键
     *
     * @return 返回是否全屏
     */
    fun backFromWindowFull(context: Context): Boolean {
        var backFrom = false
        val vp =
            CommonUtil.scanForActivity(context)
                .findViewById<View>(Window.ID_ANDROID_CONTENT) as ViewGroup
        val oldF = vp.findViewById<View>(RealVideoManager.FULLSCREEN_ID)
        if (oldF != null) {
            backFrom = true
            CommonUtil.hideNavKey(context)
            if (instance.lastListener() != null) {
                instance.lastListener().onBackFullscreen()
            }
        }
        return backFrom
    }

    /**
     * 页面销毁了记得调用是否所有的video
     */
    fun releaseAllVideos() {
        if (instance.listener() != null) {
            instance.listener().onCompletion()
        }
        instance.isNeedMute = true
        instance.releaseMediaPlayer()
    }

    /**
     * 暂停播放
     */
    fun onPause() {
        if (instance.listener() != null) {
            instance.listener().onVideoPause()
        }
    }

    /**
     * 恢复播放
     */
    fun onResume() {
        if (instance.listener() != null) {
            instance.listener().onVideoResume()
        }
    }


    /**
     * 恢复暂停状态
     *
     * @param seek 是否产生seek动作,直播设置为false
     */
    fun onResume(seek: Boolean) {
        if (instance.listener() != null) {
            instance.listener().onVideoResume(seek)
        }
    }


    override fun getPlayManager(): IPlayerManager {
        return IjkPlayerManager()
    }
}